﻿using Corina.Commands;
using Corina.Common;
using Corina.Datamodel.DataObject;
using Corina.Enumerations;
using Corina.Infrastructure;
using Corina.Win.DataService;
using Corina.Win.Helpers;
using Corina.Win.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Corina.Addins.Msgtest
{
    [Export(typeof(IView))]
    public class MsgtestViewmodel : BaseClass, IView
    {
        #region Members
        public DeviceMeasureSoapClient SoapClient { get; set; }
        #endregion

        public MsgtestViewmodel() {
            SoapClient = new DeviceMeasureSoapClient(WebBindingConfig.HttpBinding, new EndpointAddress(WebServiceAddress + ConstUrls.URL_DEVICEMEASURE));

            Title = "测试";
            UriString = "MsgtestView";
            UriType = Enumerations.UriType.InApp;
            Icon = "ICON_MEMORY_CARD";
        }

        public string WebServiceAddress
        {
            get
            {
                return ConfigurationManager.AppSettings["app_webservicehost"];
            }
        }

        #region Fields
        private int _pageIndex = 0;
        private string _title;
        private string _uriString;
        private UriType _uriType = UriType.OutApp;
        private string _icon;
        private int _index = 0;
        #endregion

        #region Properties
        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                _pageIndex = value;
                RaisePropertyChanged(() => PageIndex);
            }
        }

        public string Icon
        {
            get { return _icon; }
            set
            {
                _icon = value;
                RaisePropertyChanged(() => Icon);
            }
        }

        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        public string UriString
        {
            get { return _uriString; }
            set
            {
                _uriString = value;
                RaisePropertyChanged(() => UriString);
                RaisePropertyChanged(() => FullUrl);
            }
        }

        public UriType UriType
        {
            get { return _uriType; }
            set
            {
                _uriType = value;
                RaisePropertyChanged(() => UriType);
                RaisePropertyChanged(() => FullUrl);
            }
        }

        public string FullUrl
        {
            get
            {
                return UrlPrefix<MsgtestViewmodel>.Prefixs.FirstOrDefault(k => k.Key == UriType).Value + "Views/" + UriString + ".xaml";
            }
        }

        public int Index
        {
            get { return _index; }
            set
            {
                _index = value;
                RaisePropertyChanged(() => Index);
            }
        }
        #endregion

        #region Send
        private bool _isbusy = false;
        public bool IsBusy
        {
            get { return _isbusy; }
            set
            {
                _isbusy = value;
                RaisePropertyChanged(() => IsBusy);
            }
        }

        private string _content = string.Empty;
        public string Content
        {
            get { return _content; }
            set
            {
                _content = value;
                RaisePropertyChanged(() => Content);
            }
        }

        private ResultInfo _result = new ResultInfo(ResultValue.Unknown);
        public ResultInfo Result
        {
            get { return _result; }
            set
            {
                _result = value;
                RaisePropertyChanged(() => Result);
            }
        }

        private void Send()
        {
            IsBusy = true;
            Task<ResultInfo> task = SoapClient.InsertBatchDeviceMessageAsync("root", "7D67C8F61FEA65A94B4FF031544A7B64", "213213213", Content.Replace("", ""), "192.168.1.200");
            task.ContinueWith(t =>
            {
                if (task.IsCompleted)
                {
                    if (task.Exception == null)
                    {
                        Result = task.Result;
                    }
                    else
                    {
                        ErrorTraceHelper.Trace(task.Exception);
                        Result = new ResultInfo(ResultValue.Failed);
                    }
                }
                else
                {
                    Result = new ResultInfo(ResultValue.TimeOut);
                }
                IsBusy = false;
            });
        }

        public ICommand SendCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        Send();
                    }
                };
                
            }
        }
        #endregion
    }
}
